package com.tool.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @author 酒徒
 * @Version 1.0
 * @data 2025/4/14 18:53
 */
@Aspect
@Component
public class AspectConfig {

    @Pointcut("@annotation(com.tool.aspect.AspectHandler)")
    public void Pointcut(){}

    @Pointcut(value = "execution(public String com.tool.aspect.controller.UserController.*(..))")
    public void pointcut(){}



    @Before("Pointcut()")
    public void before(JoinPoint joinPoint){
        String str = joinPoint.getSignature().toString();
        System.out.println("before ... " + str);
    }

    @After("pointcut()")
    public void after(){
        System.out.println("after");
    }

    @Around(value = "execution(public Integer com.tool.aspect.controller.UserController.around(..))")
    public void around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("around 开始执行");
        joinPoint.proceed();
        System.out.println("around 结束执行");
    }

    @AfterReturning(value = "execution(public Integer com.tool.aspect.controller.UserController.add(..))",returning = "result")
    public void afterReturning(JoinPoint joinPoint, Integer result){
        Object[] args = joinPoint.getArgs();
        int res = (Integer) args[0] * (Integer) args[1];
        System.out.println("afterReturning ... 加：" + result);
        System.out.println("afterReturning ... 乘：" + res);
    }

    @AfterThrowing(value = "execution(public Integer com.tool.aspect.controller.UserController.exception(..))",throwing = "e")
    public void afterThrowing(Exception e){
        if (e.getMessage().contains("by zero")) {
            System.out.println("afterThrowing ... 除数不能为0");
        } else {
            System.out.println("afterThrowing ... " + e.getMessage());
        }
    }
}
